﻿using MySql.Data.MySqlClient;
using INI;
using System.Windows.Forms;
using System.Data;
using PESH_Engineer_ToolKit;

namespace Global
{
    public static class Shopfloor_DB
    {
        public static  MySqlConnection conn;
        public  static string IP_ShopfloorWeb;
        public  static string IP_ShopfloorDB;
        public  static string User_ShopfloorDB;
        public  static string Pass_ShopfloorDB;
        public  static string Database_ShopfloorDB;
        private static INIClass INI_ShopfloorSetting = new INIClass(Application.StartupPath.TrimEnd('\\') + "\\Shopfloor.ini");
        private static INIClass INI_Fields = new INIClass(Application.StartupPath.TrimEnd('\\') + "\\Fields.ini");
        #region<------------INI Operate-------->
        public static void  ReadAllFromINI(bool openconnection)
        {
            IP_ShopfloorWeb = INI_ShopfloorSetting.IniReadValue("Shopfloor", "IP_ShopfloorWeb", "127.0.0.1");
            IP_ShopfloorDB = INI_ShopfloorSetting.IniReadValue("Shopfloor", "IP_ShopfloorDB", "127.0.0.1");
            User_ShopfloorDB = INI_ShopfloorSetting.IniReadValue("Shopfloor", "User_ShopfloorDB", "root");
            Pass_ShopfloorDB = INI_ShopfloorSetting.IniReadValue("Shopfloor", "Pass_ShopfloorDB", "");
            Database_ShopfloorDB = INI_ShopfloorSetting.IniReadValue("Shopfloor", "Database_ShopfloorDB", "shopfloor");
            if (openconnection)
            {
                conn = new MySqlConnection(string.Format("server={0};uid={1};pwd={2};database={3}", IP_ShopfloorDB, User_ShopfloorDB, Pass_ShopfloorDB, Database_ShopfloorDB));
                conn.Open();
            }
        }
        public static void WriteAllToINI()
        {
            INI_ShopfloorSetting.IniWriteValue("Shopfloor", "IP_ShopfloorWeb", IP_ShopfloorWeb);
            INI_ShopfloorSetting.IniWriteValue("Shopfloor", "IP_ShopfloorDB", IP_ShopfloorDB);
            INI_ShopfloorSetting.IniWriteValue("Shopfloor", "User_ShopfloorDB", User_ShopfloorDB);
            INI_ShopfloorSetting.IniWriteValue("Shopfloor", "Pass_ShopfloorDB", Pass_ShopfloorDB);
            INI_ShopfloorSetting.IniWriteValue("Shopfloor", "Database_ShopfloorDB", Database_ShopfloorDB);
        }
        public static string GetFieldDescriptionFromINI(string TableName, string FieldName)
        {
            return INI_Fields.IniReadValue(TableName, FieldName, FieldName);
        }


        #endregion

        #region <-----------shopfloor functions----------->

        public static DataTable Query(string TableName, string Fields, string Where, string OrderBy, bool IfDESC, int limit)
        {
            string strOrder = IfDESC ? "DESC" : "ASC";
            string sql = string.Format("select {0} from {1} where {2} order by {3} {4} limit {5}", Fields, TableName, Where, OrderBy, strOrder, limit.ToString());
            if (conn.State != ConnectionState.Open) conn.Open();
            using (DataTable dt = new DataTable())
            {
                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                da.Fill(dt);
                conn.Close();
                foreach (DataColumn col in dt.Columns)
                {
                    col.ColumnName = GetFieldDescriptionFromINI(TableName, col.ColumnName);
                }
                return dt;
            }
        }
        public static DataTable Query(string TableName, string Fields, string Where, int limit)
        {
            string sql = string.Format("select {0} from {1} where {2} limit {3}", Fields, TableName, Where, limit.ToString());
            if (conn.State != ConnectionState.Open) conn.Open();
            using (DataTable dt = new DataTable())
            {
                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                da.Fill(dt);
                conn.Close();
                foreach (DataColumn col in dt.Columns)
                {
                    col.ColumnName = GetFieldDescriptionFromINI(TableName, col.ColumnName);
                }
                return dt;
            }
        }
        public static DataTable Query(string TableName, string Fields, int limit)
        {
            string sql = string.Format("select {0} from {1}  limit {2}", Fields, TableName, limit.ToString());
            if (conn.State != ConnectionState.Open) conn.Open();
            using (DataTable dt = new DataTable())
            {
                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                da.Fill(dt);
                conn.Close();
                foreach (DataColumn col in dt.Columns)
                {
                    col.ColumnName = GetFieldDescriptionFromINI(TableName, col.ColumnName);
                }
                return dt;
            }
        }
        public static DataTable Query(string SQL)
        {
            string sql = SQL;
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                using (DataTable dt = new DataTable())
                {
                    MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                    da.Fill(dt);
                    conn.Close();
                    return dt;
                }
            }
            catch (MySqlException msE)
            {
                MessageBox.Show(msE.Message);
                return null;
            }
        }

        #endregion
    }
    public static class SHOPFLOOR_Web
    {

        public static string TB100GroupToString(TB100Group myTB100Group)
        {
            string sTemp;
            string sSerial = "", sQd = "", sQdShift = "", sQs = "", sQsShift = "";

            foreach (Injector temp in myTB100Group.Injectors)
            {
                sSerial = sSerial + ',' + temp.getMatrix();
                sQd = sQd + ',' + temp.getQd().ToString();
                sQdShift = sQdShift + ',' + temp.getQdShift().ToString();
                sQs = sQs + ',' + temp.getQs().ToString();
                sQsShift = sQsShift + ',' + temp.getQsShift().ToString();

            }
            sTemp = sSerial.Substring(1) + '|';
            sTemp = sTemp + sQd.Substring(1) + '|';
            sTemp = sTemp + sQdShift.Substring(1) + '|';
            sTemp = sTemp + sQs.Substring(1) + '|';
            sTemp = sTemp + sQsShift.Substring(1) + '|';
            return sTemp;
        }
    }

}
